import { EnumAiFailedOperation, EnumMemberFailedOperation, EnumMemberMatchType, EnumMemberSuccessfulOperation, EnumScene, MemberIntentionResult, MemberIntentionStep3Request } from "@/apis/endpoint/model";
import { postMyIntentionstep3 } from "@/apis/endpoint/my";
import { validateScheme } from "@/utils/app";
import { useImmer } from "use-immer";
import Taro from "@tarojs/taro";
import { isSetValue } from "@/utils/helper";

interface StepThreeState extends Partial<MemberIntentionStep3Request> {}
export function useStepThree(next: IFn) {
  const aiFailedOptions: IOption<EnumAiFailedOperation>[] = [
    { value: EnumAiFailedOperation.待介入, label: "立即通知我，并标记待介入" },
    { value: EnumAiFailedOperation.忽略, label: "忽略问题，继续沟通其他信息" }
  ]
  const matchTypeOptions: IOption<EnumMemberMatchType>[] = [
    { value: EnumMemberMatchType.严格匹配, label: "严格匹配", desc: "必须满足所有核心条件，工作高匹配但机会变少" },
    { value: EnumMemberMatchType.均衡匹配, label: "均衡匹配", desc: "大部分核心条件满足，工作匹配度和机会较为均衡" },
    { value: EnumMemberMatchType.宽松匹配, label: "宽松匹配", desc: "只有不触发硬性拒绝条件，工作机会最多，但意向可能不够精准" }
  ]
  const rejectOptions: IOption<EnumScene>[] = [
    { value: EnumScene.工作时间, label: "属于我不接受的上班休息制度" },
    { value: EnumScene.试用期, label: "属于我不接受的试用期时长" },
    { value: EnumScene.加班, label: "属于我不接受的加班制度" },
    { value: EnumScene.工作地点, label: "属于我不接受的通勤范围外" },
    { value: EnumScene.出差, label: "属于我不接受的出差频率" },
    { value: EnumScene.底薪, label: "低于我最低可接受底薪范围" },
    { value: EnumScene.福利, label: "没有我觉得必要的福利" }
  ]
  const fitOptions: IOption<EnumMemberSuccessfulOperation>[] = [
    { value: EnumMemberSuccessfulOperation.标记匹配, label: "立即通知我，并标记匹配成功" },
    { value: EnumMemberSuccessfulOperation.发送简历, label: "自动发送我的简历给HR" },
    { value: EnumMemberSuccessfulOperation.交换微信, label: "交换微信联系" },
    { value: EnumMemberSuccessfulOperation.交换手机, label: "交换手机联系" }
  ]
  const unfitOptions: IOption<EnumMemberFailedOperation>[] = [
    { value: EnumMemberFailedOperation.忽略, label: "忽略该职位，并标记不合适" },
    { value: EnumMemberFailedOperation.婉拒, label: "婉拒该职位，并标记不合适" }
  ]
  const [stepThreeState, setStepThreeState] = useImmer(getInitialState())
  function getInitialState(): StepThreeState {
    return {
      rejectConditions: [],
      successfulOperation: [],
      failedOperation: []
    }
  }
  function receiveStepThree(setting: MemberIntentionResult) {
    const patchFields: (keyof StepThreeState)[] = ["aiFailedOperation", "failedOperation", "matchType", "rejectConditions", "successfulOperation"]
    let patch: INullable<{[key in keyof StepThreeState]: MemberIntentionResult[key]}> = null
    patchFields.forEach(field => {
      if (isSetValue(setting[field])) {
        patch ??= {}
        // @ts-ignore
        patch[field] = setting[field]
      }
    })
    if (patch) {
      setStepThreeState(draft => {
        Object.keys(patch!)
        .forEach(field => {
          draft[field] = patch![field]
        })
      })
    }
  }
  async function saveStepThree() {
    const params = {} as MemberIntentionStep3Request
    const valid = validateScheme(stepThreeState, params, {
      aiFailedOperation: "aiFailedOperation",
      failedOperation: "failedOperation",
      matchType: "matchType",
      rejectConditions: "rejectConditions",
      successfulOperation: "successfulOperation"
    }, {
      aiFailedOperation: {
        field: "aiFailedOperation",
        dataField: "aiFailedOperation",
        message: "请设置AI采取动作"
      },
      failedOperation: {
        field: "failedOperation",
        dataField: "failedOperation",
        message: "请设置不匹配职位时动作",
        validator(value) {
          return value!.length > 0
        },
      },
      matchType: {
        field: "matchType",
        dataField: "matchType",
        message: "请设置匹配模式"
      },
      rejectConditions: {
        field: "rejectConditions",
        dataField: "rejectConditions",
        message: "请设置硬性拒绝条件",
        validator(value) {
          return value!.length > 0
        },
      },
      successfulOperation: {
        field: "successfulOperation",
        dataField: "successfulOperation",
        message: "请设置匹配成功职位动作",
        validator(value) {
          return value!.length > 0
        },
      },
    })
    if (!valid) return
    await postMyIntentionstep3(params).send()
    Taro.showToast({
      icon: "success",
      title: "设置成功"
    })
    next()
  }
  return {
    stepThreeState,
    setStepThreeState,
    saveStepThree,
    aiFailedOptions,
    matchTypeOptions,
    fitOptions,
    unfitOptions,
    rejectOptions,
    receiveStepThree
  }
}
